//
//  TravelActivityTableViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "TravelActivityTableViewController.h"
#import "ActivitiesCycleModel.h"
#import "ActivitiesView.h"
#import "ActDetailViewController.h"
#import "ActDetailListTableViewController.h"
#import "EightPicView.h"
#import "ActHotTableViewCell.h"
#import "ActHotModel.h"
#import "EightDetailTableViewController.h"

@interface TravelActivityTableViewController ()<UIScrollViewDelegate>
//接收轮播图图片的数组
@property (nonatomic,strong) NSMutableArray *activitiesCycleArr;
//接收轮播图其他信息的数组
@property (nonatomic,strong) NSMutableArray *actModelArr;

@property (nonatomic,strong) ActivitiesView *CycleView;
@property (nonatomic,strong) NSMutableArray *hotArr;

@end

@implementation TravelActivityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //让区头不停留在屏幕上
    self.tableView =[self.tableView initWithFrame:kScreen style:(UITableViewStyleGrouped)];
    
    self.view.backgroundColor = kColor;
    [self.tableView registerClass:[ActHotTableViewCell class] forCellReuseIdentifier:@"HotCell"];
    
    [self JsonActivitiesCycle];
    [self setNavigation];
    
}

//设置导航条
- (void)setNavigation{
    self.navigationItem.title = @"活动";
    //38 156 197
    [self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:38%256/255 green:156%256/255 blue:197%256/255 alpha:1]];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_actionbar_search_pressed_44x44@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(Search)];
    self.navigationItem.rightBarButtonItem = item;
    
}

//搜索按钮的点击方法
- (void)Search{

}

- (NSMutableArray *)activitiesCycleArr{
    if(!_activitiesCycleArr){
        self.activitiesCycleArr = [NSMutableArray array];
    }
    return _activitiesCycleArr;
}

- (NSMutableArray *)actModelArr{
    if(!_actModelArr){
        self.actModelArr = [NSMutableArray array];
    }
    return _actModelArr;
}

//解析活动页轮播图
- (void)JsonActivitiesCycle{
    [LORequestManger GET:kActivitiesCycleUrl success:^(id response) {
        NSDictionary *mdic = (NSDictionary *)response;
        for (NSDictionary *dic in mdic[@"advertisements"]) {
            ActivitiesCycleModel *model = [[ActivitiesCycleModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.activitiesCycleArr addObject:model.share_pic];
            [self.actModelArr addObject:model];
        }
        [self SetUpCycleView];
        [self jsonAndHot];

        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (NSMutableArray *)hotArr{
    if (!_hotArr) {
        self.hotArr = [NSMutableArray array];
    }
    return _hotArr;
}

//解析首页热门活动
- (void)jsonAndHot{
    [LORequestManger GET:kHotActivitiesUrl success:^(id response) {
        NSDictionary *mdic = (NSDictionary *)response;
        
        for (NSDictionary *dic in mdic[@"activities"]) {
            ActHotModel *model = [[ActHotModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.hotArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

}


//设置轮播图
- (void)SetUpCycleView{
    self.CycleView = [[ActivitiesView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 170) imageArray:self.activitiesCycleArr delegate:self action:@selector(TouchImage) timer:3.5 selector:@selector(button)];
    self.tableView.tableHeaderView = self.CycleView;
    self.CycleView.delegate = self;
}

//轮播图的点击方法
- (void)TouchImage{
    //起始位置为下标1,我们第一张显示的图片前面还有一张图片 所以我们需要将下标-1 从0开始
    int index = self.CycleView.contentOffset.x/kWidth;
    index -= 1;

    //当我们的index为最后一张的时候 我们将index的值设为0 以便于我们可以根据下标进行相对操作(页面跳转 去数组内某一下标的值等)
    if(index == self.activitiesCycleArr.count){
        index = 0;
    }
    //if(index != 1){
        ActDetailListTableViewController *detailList = [[ActDetailListTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
        ActivitiesCycleModel *model=self.actModelArr[index];
        NSString *strUrl = [NSString stringWithFormat:@"http://tubu.la/rest/v1/advertisement/%@/activities",model.ID];
        detailList.CycleUrl = strUrl;
        detailList.picUrl = model.share_pic;
        
        [self.navigationController pushViewController:detailList animated:NO];
    //}else{
//        ActDetailViewController *ACDetail = [[ActDetailViewController alloc] init];
//        [self.navigationController pushViewController:ACDetail animated:NO];
//    }
}

//轮播图定时器的方法  设置循环滚动
- (void)button{
    CGPoint newPoint = self.CycleView.contentOffset;
    newPoint.x += kWidth;
    [self.CycleView setContentOffset:newPoint animated:YES];
    [self scrollAndButton];
}

//封装循环滚动的方法  在手动滑动与定时器方法内调用
- (void)scrollAndButton{
    //如果当前偏移量为0时表示
    if(self.CycleView.contentOffset.x == 0){
        [self.CycleView setContentOffset:CGPointMake( self.CycleView.contentSize.width - (2 * self.CycleView.bounds.size.width), 0) animated:NO];
    }else if(self.CycleView.contentOffset.x == self.CycleView.contentSize.width - self.CycleView.bounds.size.width){
        [self.CycleView setContentOffset:CGPointMake(self.CycleView.bounds.size.width, 0) animated:NO];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self scrollAndButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.hotArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActHotModel *model = self.hotArr[indexPath.row];
    
    ActHotTableViewCell *cell= [[ActHotTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"HotCell" typeArr:model.tag];
    cell.title.text = model.title;
    cell.city.text = [NSString stringWithFormat:@"%@ - %@",model.city,model.destination];
    cell.time.text = [NSString stringWithFormat:@"%@ 行程%ld天",model.period_desc,model.days];
    cell.Money.text = [NSString stringWithFormat:@"￥%@ - ￥%@",model.min_cost,model.max_cost];
    [cell.logo sd_setImageWithURL:[NSURL URLWithString:model.club[@"logo"]]];
    cell.club.text = model.club[@"title"];
    if(model.is_hot == 1){
        cell.hot.image = [UIImage imageNamed:@"img_tag_56x56"];
    }
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    return cell;
}

//点击cell触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActDetailViewController *detail = [[ActDetailViewController alloc] init];
    ActHotModel *model = self.hotArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v3/activity/%@?access_token=1d209720-f920-497e-8faf-e0e8bc7fbb0a&app_version=2.3.6&device_type=1",model.ID];
    detail.detailUrl = str;
    detail.day = model.days;
    [self.navigationController pushViewController:detail animated:NO];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}


//表头的高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 180;
}

//section的表头  将8个导航图设为分区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *pic = @[@"activity_grid_item_content_new_1@2x",@"activity_grid_item_content_new_2@2x",@"activity_grid_item_content_new_3@2x",@"activity_grid_item_content_new_4@2x",@"activity_grid_item_content_new_6@2x",@"activity_grid_item_content_new_8@2x",@"activity_grid_item_content_new_13@2x",@"activity_grid_item_content_new_13@2x"];
    NSArray *title = @[@"全部",@"周末",@"长线",@"摄影",@"入门",@"进阶",@"溯溪",@"亲子"];
    EightPicView *p=[[EightPicView alloc]init];
    EightPicView *a=[p initpic:pic title:title target:self action:@selector(tapPic:)];
    return a;
}

-(void)tapPic:(UIButton *)btn{
    NSArray *title = @[@"全部",@"周末",@"长线",@"摄影",@"入门",@"进阶",@"溯溪",@"亲子"];
    EightDetailTableViewController *tab = [[EightDetailTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    //将汉字转换成字符串
    NSString *ss = [title[btn.tag-10] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *str = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v3/activities?&page_size=20&cur_page=1&sort=0&keywords=%@&device_type=2&app_version=4.1.5&is_recent=0&api_version=1&city=3",ss];
    tab.EightUrl = str;
    [self.navigationController pushViewController:tab animated:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
    self.navigationController.tabBarController.tabBar.alpha = 1;
}

@end
